/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sands;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;
import java.util.List;

/**
 *
 * @author Admin
 */
public class ListNemosActivity extends Activity implements View.OnClickListener,View.OnLongClickListener {

    /**
     * Called when the activity is first created.
     */
    private DatabaseHelper helper;
    private TableLayout tableLayout ;
    private LayoutInflater inflater ;
    @Override
    public void onCreate(Bundle icicle){
	super.onCreate(icicle);
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	init();
    }
    
    private void init() {
	try{
	    setContentView(R.layout.list_nemo);
	    TableLayout tableLayoutHeader = (TableLayout) findViewById(R.id.listnemosheader);
	    tableLayoutHeader.addView(addRow("Url","User name",1)) ;
	    
	    tableLayout = (TableLayout) findViewById(R.id.listnemos);
	    helper = DatabaseHelper.getInstance(this);
	    List<Nemo> nemos = helper.getNemos();
	    for(int i = 0; i < nemos.size();i++){
		Nemo nemo_ = nemos.get(i);
		TableRow row1 = addRow(nemo_.getUrl(),nemo_.getuName(),i);
		row1.setTag(nemo_.getUrl()+";"+nemo_.getPwd());
		row1.setOnClickListener(this);
		row1.setOnLongClickListener(this);
		tableLayout.addView(row1);
	    }
	}
	catch(Exception e){
	    Log.e("ListNemo", "error", e);
	}
    }
    
    
    private TableRow addRow(String column1val, String column2val , int i ) {
	if(inflater == null) {
	    inflater = LayoutInflater.from(this);
	}
	View view = inflater.inflate(R.layout.nemo_row,null);
	TableRow rowHeader = (TableRow) view.findViewById(R.id.table_row);
	
	String clr1 = i%2==0? "#FFBADE": "#73BE94";
	String clr2 = i%2==0? "#73BE94": "#FFBADE";
	
	TextView v1 = (TextView) view.findViewById(R.id.row_text_url);
	TextView v2 = (TextView) view.findViewById(R.id.row_text_uname);
	setContentForView(column1val, clr1, v1);
	setContentForView(column2val, clr2, v2);
	return  rowHeader;
    }
    
    private void setContentForView(String value,String bg, TextView valueText){
	valueText.setTextColor(Color.BLACK);
	valueText.setBackgroundColor(Color.parseColor(bg));
	valueText.setText(value);
	valueText.setTextSize(15);
	valueText.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD);
	valueText.setHeight(40);
	valueText.setGravity(Gravity.LEFT);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
	MenuItem newTicket = menu.add(Menu.NONE, Menu.NONE, 0, "Add New");
	newTicket.setOnMenuItemClickListener(new OnMenuItemClickListener(){
	    public boolean onMenuItemClick(MenuItem clickedItem){
		Intent newIntent = new Intent(ListNemosActivity.this, NewNemoActivity.class);
		startActivityForResult(newIntent,123);
		return true;
	    }
	});

	return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
	if(resultCode == RESULT_OK) {
	    init();
	}
    }

    
    public void onClick(View view){
	TableRow row = (TableRow) view ;
	row.setSelected(true);
	String urlpwd = (String)row.getTag() ;
	String pwd = urlpwd.split(";")[1];
	Toast.makeText(ListNemosActivity.this, "[" + pwd+"]", Toast.LENGTH_LONG).show();
    }

    public boolean onLongClick(View view){
	TableRow row = (TableRow) view;
	String urlpwd = (String) row.getTag();
	final String url = urlpwd.split(";")[0];
	final CharSequence[] items = {"Edit", "Delete"};

	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	builder.setTitle("Nemo...");
	builder.setItems(items, new DialogInterface.OnClickListener(){

	    public void onClick(DialogInterface dialog, int item){
		if(item == 0){
		    Toast.makeText(ListNemosActivity.this, "Edit pressed", Toast.LENGTH_LONG).show();
		}
		else if(item == 1){
		    String result = helper.deleteNemo(url);
		    Toast.makeText(ListNemosActivity.this, result, Toast.LENGTH_LONG).show();
		    init();
		}
	    }
	});
	AlertDialog alert = builder.create();
	alert.show();
	return true;
    }
    
    @Override
    public void onBackPressed(){
	SQLiteDatabase db = DatabaseHelper.getInstance(this).getReadableDatabase();
	if(db.isOpen()){
	    db.close();
	}
	finish();
    }
}
